<template>
    <div>
        <h1 style="text-align: center;">权限添加</h1>
        <el-form ref="form" :model="form" label-width="80px">
            <el-form-item label="权限名称">
                <el-input v-model="form.name"></el-input>
            </el-form-item>
            <el-form-item label="父权限">
                <el-select v-model="form.parentId" placeholder="请选择父级">
                    <el-option label="无父级" value="0"></el-option>
                    <el-option v-for="auth in auths" :key="auth._id" :label="auth.name" :value="auth._id"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="权限路径">
                <el-input v-model="form.path"></el-input>
            </el-form-item>
            <el-form-item label="权限图标">
                <el-input v-model="form.icon"></el-input>
            </el-form-item>
            <el-form-item label="是否显示">
                <el-switch v-model="form.isShow"></el-switch>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="add">立即添加</el-button>
                <el-button>取消</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
import { authList, authAdd } from '@/request'

export default {
    data() {
        return {
            form: {
                name: '',
                path: '',
                parentId: '',
                icon: '',
                isShow: false
            },
            auths: []
        }
    },
    methods: {
        async getAuthList() {
            let data = await authList()
            this.auths = data.data.authlist
        },
        async add() {
            let data = await authAdd(this.form)
            let { msg, error_code } = data
            if (error_code === 200) {
                this.$message({
                    message: msg,
                    type: 'success',
                    duration: 1500,
                    onClose: () => {
                        this.$router.push('/auth/list')
                    }
                })
            } else {
                his.$message({
                    message: msg,
                    type: 'error'
                })
            }
        }
    },
    created() {
        this.getAuthList()
    }
}
</script>

<style></style>
